<template>
  <div class="JNPF-common-layout">
    <div class="JNPF-common-layout-center">
      <el-row class="JNPF-common-search-box" :gutter="16">
        <el-form @submit.native.prevent label-width="80px" label-position="left">
          <el-col :span="6">
            <el-form-item label="上级菜单">
        <el-select v-model="listQuery.pid" clearable filterable class="filter-item" placeholder="请选择上级菜单">
          <el-option
            v-for="item in list2"
            :key="item.id"
            :label="item.name"
            :value="item.id"/>
        </el-select>
      </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="菜单名称">
        <el-input v-model="listQuery.name" clearable class="filter-item" style="width: 200px;" placeholder="请输入菜单名称"/>
      </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item>
        <el-button type="primary" @click="handleFilter">查找</el-button>
        <el-button type="success" @click="handleCreate">添加</el-button>
      </el-form-item>
          </el-col>
          <el-col :span="6"></el-col>
        </el-form>
      </el-row>
      <div class="JNPF-common-layout-main JNPF-flex-main">
       <!-- 查询结果 -->
    <JNPF-table   v-loading="listLoading" :data="list">
        <el-table-column :width="buttonColor.operationTwo" align="center" label="操作" class-name="small-padding">
        <template slot-scope="scope">
          <el-button type="text" @click="handleUpdate(scope.row)">编辑</el-button>
          <el-button type="text" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
      <el-table-column :width="buttonColor.line" :index="indexMethod" align="center" label="序号" type="index" />
      <el-table-column :formatter="formatSite" align="left" label="上级菜单" prop="pid" />
      <el-table-column align="left" label="菜单名称" prop="name" />
      <el-table-column align="left" label="文件目录" prop="value" />
      <el-table-column width="60" align="center" label="层级" prop="type" />
      <el-table-column align="left" label="窗体名称" prop="uri" />
      <el-table-column width="80" align="left" label="状态" prop="status">
        <template slot-scope="scope">
          <span :style="{color:buttonColor.statedisable}" v-if="scope.row.status===0">{{ scope.row.status | statusFilter }}</span>
          <span :style="{color:buttonColor.statestart}" v-else>{{ scope.row.status | statusFilter }}</span>
        </template>
      </el-table-column>
      <el-table-column :width="buttonColor.num" align="center" label="排序" prop="sortSeq" />
    </JNPF-table>
    <paginations
	:total="total"
	:page.sync="listQuery.currentPage"
	:limit.sync="listQuery.pageSize"
	/>
      </div>
    </div>
    <!-- 添加或修改对话框 -->
    <el-dialog :close-on-click-modal="false" top="10px" :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" width="50%">
      <el-row class="JNPF-common-search-box" :gutter="16">
        <el-form @submit.native.prevent label-width="80px" label-position="left" ref="dataForm" :rules="rules" :model="dataForm" >
          <el-col :span="24">
            <el-form-item label="上级菜单" prop="pid">
          <el-select v-model="dataForm.pid" clearable filterable placeholder="请选择上级菜单">
            <el-option
              v-for="item in list2"
              :key="item.id"
              :label="item.name"
              :value="item.id"/>
          </el-select>
        </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="菜单名称" prop="name">
          <el-input v-model="dataForm.name"/>
        </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="文件目录" prop="value">
          <el-input v-model="dataForm.value"/>
        </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="菜单层级" prop="type">
          <el-input v-model.number="dataForm.type"/>
        </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="窗体名称" prop="uri">
          <el-input v-model="dataForm.uri"/>
        </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item class="el-form-item-small" label="状态" prop="status">
          <el-select v-model="dataForm.status" class="filter-item" placeholder="请选择状态">
            <el-option v-for="item in fbillstate" :key="item.value" :label="item.label" :value="item.value"/>
          </el-select>
        </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="排序" prop="sortSeq">
          <el-input v-model.number="dataForm.sortSeq"/>
        </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="图标" prop="icon">
          <el-input v-model="dataForm.icon"/>
        </el-form-item>
          </el-col>
        </el-form>
      </el-row>
      <span slot="footer" class="dialog-footer">
        <el-button type="info" @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" v-if="dialogStatus=='create'" @click="createData">确定</el-button>
        <el-button type="primary" v-else  @click="updateData">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { listsysPermission, createsysPermission, updatesysPermission, deletesysPermission } from '@/api/sysPermission'
import Pagination from '@/components/Pagination'
import moment from 'moment'
import { tableColor, titleColor,buttonColor } from '@/filters/index'
const statusMap = {
  0: '禁用',
  1: '启用'
}
export default {
  name: 'AmSysPermission',
  components: { Pagination },
  filters: {
    statusFilter(type) {
      return statusMap[type]
    }
  },
  data() {
    return {
      tableColor,
      titleColor,
      buttonColor,
      list: null,
      list2: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        pid: undefined,
        name: undefined,
        value: undefined,
        icon: undefined,
        type: undefined,
        uri: undefined,
        status: undefined,
        sortSeq: undefined,
        method: undefined,
        icon: undefined,
        sort: 'create_time',
        order: 'desc'
      },
      listQuery2: {
        type: undefined,
        sort: 'create_time',
        order: 'desc'
      },
      fbillstate: [
        { value: 1, label: '启用' },
        { value: 0, label: '禁用' },
      ],
      dataForm: {
        pid: undefined,
        name: undefined,
        value: undefined,
        icon: undefined,
        type: undefined,
        uri: undefined,
        status: undefined,
        sortSeq: undefined,
        method: undefined,
        icon: undefined,
        id: undefined
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '创建'
      },
      rules: {
        pid:[
          { required: true, message: '上级菜单不能为空' },
        ],
        name:[
          { required: true, message: '菜单名称不能为空' },
        ],
        pid: [
          { type: 'number', message: '父级权限id必须为数字值' },
        ],
        type: [
          { type: 'number', message: '菜单层级必须为数字值' },
          { required: true, message: '菜单层级不能为空' },
        ],
        uri:[
          { required: true, message: '窗体名称不能为空' },
        ],
        status: [
          { type: 'number', message: '启用状态必须为数字值' },
        ],
        sortSeq: [
          { type: 'number', message: '排序必须为数字值' },
        ],
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    datetimeFormat(row, column) {
      const date = row[column.property]
      if (date === undefined || date === null) {
        return ''
      }
      return moment(date).format('YYYY-MM-DD HH:mm:ss')
    },
    getList() {
      this.listLoading = true
      listsysPermission(this.listQuery)
        .then(response => {
          this.list = response.data.data.list
          this.total = response.data.data.total
          this.listLoading = false
        })
        .catch(() => {
          this.list = []
          this.total = 0
          this.listLoading = false
        })
      this.listQuery2.type = 4
      listsysPermission(this.listQuery2)
        .then(response => {
          this.list2 = response.data.data.list
          // this.total = response.data.data.total
          // this.listLoading = false
        })
        .catch(() => {
          this.list2 = []
          // this.total = 0
          // this.listLoading = false
        })
    },
    formatSite: function(row, column) {
      if(this.list2){
        for (let i = 0; i < this.list2.length; i++) {
          if (row.pid === this.list2[i].id) {
            return this.list2[i].name
          }
        }
        return ''
      }else{
        return ''
      }

    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    resetForm() {
      this.dataForm = {
        pid: undefined,
        name: undefined,
        value: undefined,
        icon: undefined,
        type: undefined,
        uri: undefined,
        status: undefined,
        sortSeq: undefined,
        method: undefined,
        icon: undefined,
        id: undefined
      }
    },
    handleCreate() {
      this.resetForm()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.dataForm.status = 1
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          if (this.dataForm.pid === undefined || this.dataForm.pid == null) {
            this.dataForm.pid = 0
          }
          createsysPermission(this.dataForm)
            .then(response => {
              this.list.unshift(response.data.data)
              this.dialogFormVisible = false
              this.$notify.success({
                title: '成功',
                message: '添加菜单成功'
              })
            })
            .catch(response => {
              this.$notify.error({
                title: '失败',
                message: response
              })
            })
        }
      })
    },
    handleUpdate(row) {
      this.dataForm = Object.assign({}, row)
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateData() {
      this.$refs['dataForm'].validate(valid => {
        if (valid) {
          updatesysPermission(this.dataForm, this.$route.query.id)
            .then(() => {
              for (const v of this.list) {
                if (v.id === this.dataForm.id) {
                  const index = this.list.indexOf(v)
                  this.list.splice(index, 1, this.dataForm)
                  break
                }
              }
              this.dialogFormVisible = false
              this.$notify.success({
                title: '成功',
                message: '更新菜单成功'
              })
            })
            .catch(response => {
              this.$notify.error({
                title: '失败',
                message: response
              })
            })
        }
      })
    },
    handleDelete(row) {
      this.$confirm('是否要删除该菜单', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deletesysPermission(row)
          .then(response => {
            this.$notify.success({
              title: '成功',
              message: '删除菜单成功'
            })
            const index = this.list.indexOf(row)
            this.list.splice(index, 1)
          })
          .catch(response => {
            this.$notify.error({
              title: '失败',
              message: response
            })
          })
      })
    },
    indexMethod(index) {
      index = index + 1
      return index
    }
  }
}
</script>